package com.beta.cat.utils.sequence.builder;

import com.beta.cat.service.RedissonService;
import com.beta.cat.utils.sequence.range.impl.redis.RedisRandomSeqRangeMgr;
import com.beta.cat.utils.sequence.seq.Sequence;
import com.beta.cat.utils.sequence.seq.impl.DefaultRangeSequence;

/**
 * @author taofucheng
 * @date 2024/4/9
 */
public class RedisRadomSeqBuilder implements SeqBuilder {
    /**
     * redis服务
     */
    private RedissonService redissonService;
    /**
     * 业务名称[必选]
     */
    private String bizName;
    /**
     * 生成的序列号的最大长度，最低不能少于：5位
     */
    private int length=10;
    public Sequence build() {
        //利用Redis获取区间管理器
        RedisRandomSeqRangeMgr redisSeqRangeMgr = new RedisRandomSeqRangeMgr();
        redisSeqRangeMgr.setRedissonService(this.redissonService);
        redisSeqRangeMgr.setLength(this.length);
        redisSeqRangeMgr.init();
        //构建序列号生成器
        DefaultRangeSequence sequence = new DefaultRangeSequence();
        sequence.setName(this.bizName);
        sequence.setSeqRangeMgr(redisSeqRangeMgr);
        return sequence;
    }

    public static RedisRadomSeqBuilder create() {
        RedisRadomSeqBuilder builder = new RedisRadomSeqBuilder();
        return builder;
    }

    public RedisRadomSeqBuilder redissonService(RedissonService redissonService) {
        this.redissonService = redissonService;
        return this;
    }

    public RedisRadomSeqBuilder bizName(String bizName) {
        this.bizName = bizName;
        return this;
    }

    public RedisRadomSeqBuilder length(int length) {
        this.length = length;
        return this;
    }
}
